package com.gitee.huxing.algorithmic.daily;

import java.util.*;

/**
 * @Description: 两个列表的最小索引总和
 * @ProjectName: AlgorithmicPactice
 * @Package: com.gitee.huxing.algorithmic.daily
 * @ClassName: FindRestaurant
 * @Author: huxing
 * @DateTime: 2022-03-14 9:38 AM
 */
public class FindRestaurant {

    public static void main(String[] args) {
        String[] list1 = new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"};
        String[] list2 = new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"};
        System.out.println(Arrays.toString(findRestaurant(list1, list2)));
    }

    public static String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        for(int i = 0; i < list1.length; i++){
            map.put(list1[i], i);
        }
        int min = Integer.MAX_VALUE;
        List<String> ans = new ArrayList<String>();
        for(int i = 0; i < list2.length; i++){
            if(!map.containsKey(list2[i])){
                continue;
            }
            if(i + map.get(list2[i]) < min){
                ans.clear();
                min = i + map.get(list2[i]);
                ans.add(list2[i]);
            } else if (i + map.get(list2[i]) == min){
                ans.add(list2[i]);
            }
        }
        return ans.toArray(new String[0]);
    }
}
